package cn.yfj.org.service.impl;


import cn.yfj.basic.service.impl.BaseServiceImpl;
import cn.yfj.basic.util.MD5Utils;
import cn.yfj.basic.util.StrUtils;
import cn.yfj.org.domain.Employee;
import cn.yfj.org.mapper.EmployeeMapper;
import cn.yfj.org.service.IEmployeeServiceI;
import cn.yfj.user.domain.Logininfo;
import cn.yfj.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImplService extends BaseServiceImpl<Employee> implements IEmployeeServiceI {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    //1.基础的CRUD接口的实现  通过继承得到

    //2.扩展的接口的实现在这里自己写


    /**
     * 一下操作，自研
     * 我们在操作employee表的时候，如果涉及到登录相关信息的修改，需要级联调整logininfo表
     *
     */
    @Override
    public void save(Employee employee) {
        initEmployee(employee);
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    private void initEmployee(Employee employee) {
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        //使用Md5 加密 加盐
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

}
